package info.jimao.newsdk.results;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import info.jimao.newsdk.models.Pager;
import info.jimao.newsdk.utilities.DateDeserializer;

/**
 * 分页查询结果类<br>
 * 用于转换分页查询接口返回的数据
 *
 * @param <T> 返回的数据列表datas中的数据类型
 */
public class PageDataResult<T> implements Serializable {
    private static final long serialVersionUID = 4072691263524436177L;

    protected boolean success;
    protected String message;
    protected String errorCode;
    protected String accessToken;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    private List<T> datas;
    private Pager pager;
    Class<T> type;

    public PageDataResult(boolean success, String message) {
        this.success = success;
        this.message = message;
        this.datas = new ArrayList<T>();
        this.pager = new Pager();
    }

    public PageDataResult(String jsonString, Class<T> type) {
        this(jsonString, type, new DateDeserializer());
    }

    public PageDataResult(String jsonString, Class<T> type, JsonDeserializer<Date> dateDeserializer) {
        this.type = type;
        try {
            JSONObject root = new JSONObject(jsonString);

            this.success = !root.isNull("Success") && root.getBoolean("Success");
            this.message = !root.isNull("Message") ? root.getString("Message") : "";
            this.errorCode = !root.isNull("ErrorCode") ? root.getString("ErrorCode") : "";
            this.accessToken = !root.isNull("AccessToken") ? root.getString("AccessToken") : "";

            if (this.success) {
                Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, dateDeserializer).create();

                JSONArray jsonDatas;
                if (!root.isNull("Datas")) {
                    jsonDatas = root.getJSONArray("Datas");
                } else {
                    jsonDatas = new JSONArray() {
                    };
                }

                List<T> datas = new ArrayList<T>();
                for (int i = 0; i < jsonDatas.length(); i++) {
                    JSONObject jsonData = jsonDatas.getJSONObject(i);
                    T t = gson.fromJson(jsonData.toString(), this.type);
                    datas.add(t);
                }

                Pager pager = gson.fromJson(root.getJSONObject("Pager").toString(), Pager.class);

                this.datas = datas;
                this.pager = pager;
            }
        } catch (Exception e) {
            this.success = false;
            this.message = "数据转换异常";
            e.printStackTrace();
        }
    }

    public List<T> getDatas() {
        return datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public Pager getPager() {
        return pager;
    }

    public void setPager(Pager pager) {
        this.pager = pager;
    }

}
